package com.example.androidkt.fifth

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.lifecycle.ViewModelLazy
import androidx.lifecycle.ViewModelProvider
import com.example.androidkt.databinding.ActivityViewModelBinding

class VIewModelActivity : AppCompatActivity() {

    private val TAG = this.javaClass.simpleName

    //ViewModel初始化
    val viewModelOne by ViewModelLazy(ViewModelOne::class, { viewModelStore }, { defaultViewModelProviderFactory })

    val viewModelOnes by viewModels<ViewModelOne > { defaultViewModelProviderFactory }

    val viewModelTwo by viewModels<ViewModelTwo > { VIewModelFactory.VMfactroy(10) }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val binding = ActivityViewModelBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val viewmodelPro = ViewModelProvider(
            viewModelStore,
            defaultViewModelProviderFactory
        ).get(ViewModelOne::class.java)

        Log.d(TAG, "viewModelOne: ${viewModelOne.getNow()}")
        Log.d(TAG, "viewModelOnes: ${viewModelOnes.getNow()}")
        Log.d(TAG, "viewmodelPro: ${viewmodelPro.getNow()}")
        Log.d(TAG, "viewmodelPro: ${viewModelTwo.getNow()}")

        binding.btnAgain.setOnClickListener {
            startActivity(Intent(this,VIewModelActivity::class.java))
        }
    }
}